package com.syezon.android.base.download;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DownloadOpenHelper extends SQLiteOpenHelper {

    public static final String ID = "DOWNLOAD_ID";
    public static final String TOTAL_SIZE = "TOTAL_SIZE";
    public static final String COMPLETED_SIZE = "COMPLETED_SIZE";
    public static final String URL = "URL";
    public static final String SAVE_DIR_PATH = "SAVE_DIR_PATH";
    public static final String FILENAME = "FILE_NAME";
    public static final String DOWNLOAD_STATUS = "DOWNLOAD_STATUS";
    public static final String TABLE_NAME = "table_download";
    private static final String DATABASE_NAME = "file_download.db";
    private static final int DATABASE_VERSION = 1;

    public DownloadOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTable(db, TABLE_NAME, false);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    /**
     * Creates the underlying database table.
     */
    public void createTable(SQLiteDatabase db, String tableName, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        String sql = "CREATE TABLE " + constraint + tableName + "(" + //
                ID + " TEXT PRIMARY KEY NOT NULL ," + // 0: downloadId
                TOTAL_SIZE + " INTEGER," + // 1: totalSize
                COMPLETED_SIZE + " INTEGER," + // 2: completedSize
                URL + " TEXT," + // 3: url
                SAVE_DIR_PATH + " TEXT," + // 4: saveDirPath
                FILENAME + " TEXT," + // 5: fileName
                DOWNLOAD_STATUS + " INTEGER);"; // 6: downloadStatus
        db.execSQL(sql);
    }
}
